var removeDataSourceId;//要删除的DataSource的id

var applyDatasourceTable;

var params = {
    pageSize: 10
};

$(document).ready(function () {

    LTS.colFormatter.optFormat = function (v, row) {
    	/*if(v == 0){
    		return generateAppOptButtons(row);
    	}else{
    		return generateAppNewOptButtons(row);
    	}*/
        
    }

    LTS.colFormatter.emptyString = function (v, row) {
        return emptyString(v);
    }
    
    LTS.colFormatter.statusFormat = function(v,row){
    	return statusFormatName(row);
    }
    
    LTS.colFormatter.formTypeFormat = function(v,row){
    	return getFormTypeName(row);
    }
    
    LTS.colFormatter.tableStructFormat = function(v,row){
    	return getTableStructFormat(row);
    }
    LTS.colFormatter.tableSchemaFormat = function(v,row){
    	return getTableSchemaFormat(row);
    }
    
    applyDatasourceTable = $("#apply-datasource-table").ltsTable({
        url: getUrlBySuffix("/datasource/findDataApplyPage"),
        templateId: 'apply-datasource-table-template'
    });

    applyDatasourceTable.post(params, 1);

    
    clickCommitOABtn();
    clickRemoveBtn();
    clickConfirmRemoveBtn();
    looKoaProgress();
    /*clickUpdateBtn();
    clickConfirmUpdateBtn();*/
    
    
});
/**
 * 获取表单类型中文名称
 * */
function getFormTypeName(data){
	var typeInt = data['formType'];
	return switchFormName(typeInt);
}
/**
 * 选择表单名称
 * */
function switchFormName(typeInt){
	typeInt = $.trim(typeInt);
	var formType;
    switch (typeInt) {
        case "1":
        	formType = "开发人员申请权限";
            break;
        case "3":
        	formType = "开发人员追加权限";
            break;
        case "5":
        	formType = "开发人员申请加入权限组";
            break;
        case "7":
        	formType = "应用程序申请权限";
            break;
        case "9":
        	formType = "应用程序追加权限";
            break;
        case "11":
        	formType = "应用程序申请追加服务器";
            break;
    }
    return formType;
}

/**
 * 查询状态名称
 */
function statusFormatName(data){
	var statusInt = data['status'];
	return switchStatus(statusInt);
}
function switchStatus(statusInt){
	statusInt = $.trim(statusInt);
	var statusName;
	switch(statusInt){
		case "0":
			statusName="待提交";
			break;
		case "2":
			statusName = "待批准";
			break;
		case "4":
			statusName = "批准未通过";
			break;
		case "8":
			statusName = "已批准";
			break;
		case "16":
			statusName = "DBA执行中";
			break;
		case "32":
			statusName = "执行失败";
			break;
		case "64":
			statusName = "执行成功";
			break;
		case "128":
			statusName = "作废";
			break;
	}
	return statusName;
}
function generateAppOptButtons(data){
	var optButtons = "" +  removeBtn + "" +
    '<a href="javascript:;" class="commitOA-btn">' +
    '<span class="label label-success" style="font-size: 14px">' +
    '<i class="fa fa-eye"></i>&nbsp;&nbsp;' +
    '提交OA' +
    '</span>' +
    '</a>&nbsp;';
	return optButtons;
}
function generateAppNewOptButtons(data){
	var optButtons = ""+'<a href="javascript:;" class="">' +
    '<span class="label label-success" style="font-size: 14px">' +
    '<i class="fa fa-zidingyi"></i>&nbsp;&nbsp;' +
    '已提交OA给申请人' +
    '</span>' + '</a>&nbsp;'
    +'<a href="javascript:;" class="lookOaProgressBtn">'
	+ '<span class="label label-success" style="font-size: 14px">'
	+ '<i class="fa fa-eye"></i>&nbsp;&nbsp;' + '查看进度' + '</span>'
	+ '</a>&nbsp;';
	return optButtons;
}
function clickCommitOABtn(){
	$(document).on("click",".commitOA-btn",function(){
		$("#oaLoadingGenerator").modal("show");
		var id = getCurrentId($(this));
		var url = getRoot() + '/datasource/ajaxCommitOaInterface';
		$.post(url,{"oaMId":id},function(data){
			if(data.success){
				$("#oaLoadingGenerator").modal("hide");
				swal("执行成功",data.msg+"","success");
				applyDatasourceTable.post({pageSize: 10}, pageNum);
			}
			if(!data.success){
				$("#oaLoadingGenerator").modal("hide");
				swal("错误",data.msg+"","error");
				return;
			}
		});
	});
}
/**
 * 点击删除按钮
 */
function clickRemoveBtn() {
    $(document).on("click", ".remove-btn", function () {
        removeDataSourceId = getCurrentId($(this));
        $("#remove-tip-modal").modal("show");
    });
}

/**
 * 点击确认删除按钮
 */
function clickConfirmRemoveBtn() {
    $(document).on("click", "#confirm-remove-btn", function () {
        $("#cancel-remove-btn").trigger("click");//提示框隐藏
        var params = {};
        params['id'] = $.trim(removeDataSourceId);
        $.postHttp(params, getUrlBySuffix("/datasource/removeApplyDataSource"), function(json){
        	if(json.success){
        		swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				applyDatasourceTable.post({pageSize: 10}, pageNum);
    				$("#remove-tip-modal").modal("hide");
    			});
			}else{
				swal("错误",data.msg+"","error");
				return;
			}
        	
        });
    });
}

function getTableStructFormat(data){
	var tableJson = data['tableStructJson'];
	if(!isEmpty(tableJson)){
		var struct = JSON.parse(tableJson);
		var startStr = "";
		for(var i in struct){
			var obj = struct[i];
			startStr += obj.table_name + "[";
			if(obj.i == 'Y') startStr += "增";
			if(obj.d == 'Y') startStr += "删";
			if(obj.u == 'Y') startStr += "改";
			if(obj.s == 'Y') startStr += "查";
			startStr += "]\n";
		}
		return startStr;
	}else{
		return "无";
	}
}

function getTableSchemaFormat(data){
	var tableJson = data['tableStructJson'];
	if(!isEmpty(tableJson)){
		var struct = JSON.parse(tableJson);
		var startStr = "";
		for(var i in struct){
			var obj = struct[i];
			if(!(""==obj.schema)){
				startStr=obj.schema;
				break;
			}
		} 
		return startStr;
	}else{
		return "无";
	}
}
function looKoaProgress(){
	$(document)
	.on(
			"click",
			".lookOaProgressBtn",
			function() {
				var url = getRoot()
						+ '/datasource/findOAStatusCheckProgress';
				var params = {};
				params['oaId'] = getCurrentId($(this));
				var statusLists = new Array();
				$
						.post(
								url,
								params,
								function(data) {
									if (data.success) {
										statusLists = data.sqlRecordLists;
										var tableStart = "<table class='table table-stripped toggle-arrow-tiny footable' data-page-size='100' style='font-size: 14px'>"
												+ "<thead><tr><th>id</th><th>DBA处理状态</th><th>处理时间</th><th>执行信息</th><th>负责人审核状态</th><th>审核时间</th>"
												+ "<th>审核信息</th></tr></thead><tbody>";
										var tableContent = "";

										if (statusLists.length == 0) {
											tableContent += "<tr><td colspan='7'>暂无数据</td></tr>";
										} else {
											for ( var i in statusLists) {
												var dealStatus = switchStatus(statusLists[i].dealStatus);
												var checkStatus = switchStatus(statusLists[i].checkStatus);
												var dealTime;
												if (!statusLists[i].dealTime) {
													dealTime = "暂无";
												} else {
													dealTime = statusLists[i].dealTime;
												}
												var dbaMsg;
												if (!statusLists[i].dbaMsg) {
													dbaMsg = "暂无";
												} else {
													dbaMsg = statusLists[i].dbaMsg;
												}
												var checkTime;
												if (!statusLists[i].checkTime) {
													checkTime = "暂无";
												} else {
													checkTime = statusLists[i].checkTime;
												}
												var checkMsg;
												if (!statusLists[i].checkMsg) {
													checkMsg = "暂无";
												} else {
													checkMsg = statusLists[i].checkMsg;
												}
												if (!dealStatus) {
													dealStatus = "暂无";
												}
												if (!checkStatus) {
													checkStatus = "暂无";
												}
												var tdHtml = "<tr><td class='id-tr'><span class='id-span'>"
														+ statusLists[i].id
														+ "</span></td>"
														+ "<td>"
														+ dealStatus
														+ "</td><td>"
														+ dealTime
														+ "</td>"
														+ "<td>"
														+ dbaMsg
														+ "</td><td>"
														+ checkStatus
														+ "</td><td>"
														+ checkTime
														+ "</td>"
														+ "<td>"
														+ checkMsg
														+ "</td>";
												tableContent += tdHtml
														+ "</tr>";
											}
										}

										var tableEnd = "</tbody></table>"

										var tableHtml = tableStart
												+ tableContent
												+ tableEnd;

										$("#checkStatusTable").empty();
										$("#checkStatusTable").append(
												tableHtml);
										$("#check-status-modal").modal(
												"show");
									} else {
										if (data.failure) {
											swal("失败", data.msg,
													"error");
											return;
										}
									}
								});
			});
}
